<template>
    <div>
        <el-breadcrumb separator="/">
            <el-breadcrumb-item :to="{ path: '#' }">
                运动会基本信息设置
            </el-breadcrumb-item>
            <el-breadcrumb-item>
                <a href="#">组别基础信息</a>
            </el-breadcrumb-item>
        </el-breadcrumb>
        <el-card class="box-card">
            <div class="box-top">
                <el-button type="warning" @click="adddialogVisible = true">新增</el-button>
            </div>
            <el-table :data="currentPageData" border stripe>
                <el-table-column prop="grid" label="编号" width="60">
                </el-table-column>
                <el-table-column prop="name" label="组别">
                </el-table-column>
                <!-- <el-table-column prop="gsize" label="团体名次">
                </el-table-column>
                <el-table-column prop="gsx" label="男女分">
                    <template #default="scope">
                        {{ scope.row.gsx === 0 ? '' : '√' }}
                    </template>
</el-table-column>
<el-table-column prop="gminus" label="不足减几">
</el-table-column> -->
                <el-table-column prop="rsat" label="达标标准">
                </el-table-column>
                <el-table-column label="操作" width="230">
                    <template #default="scope">
                        <el-button type="primary" @click="showeditdialog(scope.row)">修改</el-button>
                        <el-button type="danger" @click="grodelete(scope.row)">删除</el-button>
                    </template>
                </el-table-column>
            </el-table>

            <el-pagination class="paging" @size-change="handleSizeChange" @current-change="handlePageChange" background
                :page-sizes="[3, 5, 10, 15, 20, 50]" layout="total,prev, pager, next, jumper" :total="total"
                :page-size="queryObj.limit" :current-page="queryObj.page">
            </el-pagination>
        </el-card>

        <!-- 添加页面 -->
        <el-dialog title="新增组别" v-model="adddialogVisible" draggable width="40%" :before-close="addleClose">
            <el-form :model="addfrom" :rules="rules" ref="addForm" label-width="90">
                <el-form-item prop="grid" label="编号：">
                    <el-input v-model="addfrom.grid"></el-input>
                </el-form-item>
                <el-form-item prop="name" label="组别：">
                    <el-input v-model="addfrom.name"></el-input>
                </el-form-item>
                <!-- <el-form-item label="名次：">
                    <el-input v-model="addfrom.gsize"></el-input>
                </el-form-item>
                <el-form-item label="男女分：">
                    <el-checkbox v-model="addgsx" @change="addleCheckboxChange"></el-checkbox>
                </el-form-item>
                <el-form-item label="不足减几：">
                    <el-input v-model="addfrom.gminus"></el-input>
                </el-form-item> -->
                <el-form-item label="达标标准：">
                    <el-select v-model="addfrom.rsat">
                        <el-option label=" " value=""></el-option>
                        <el-option label="高校达标标准" value="高校达标标准"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addleClose">取 消</el-button>
                <el-button type="primary" @click="addgro">确 定</el-button>
            </div>
        </el-dialog>

        <!-- 修改页面 -->
        <el-dialog title="组别信息修改" v-model="editdialogVisible" draggable width="40%" :before-close="handleClose">
            <el-form :model="editfrom" label-width="90">
                <el-form-item label="编号：">
                    <span>{{ editfrom.grid }}</span>
                </el-form-item>
                <el-form-item label="组别：">
                    <el-input v-model="editfrom.name"></el-input>
                </el-form-item>
                <!-- <el-form-item label="名次：">
                    <el-input v-model="editfrom.gsize"></el-input>
                </el-form-item>
                <el-form-item label="男女分：">
                    <el-checkbox v-model="editgsx" @change="editleCheckboxChange"></el-checkbox>
                </el-form-item>
                <el-form-item label="不足减几：">
                    <el-input v-model="editfrom.gminus"></el-input>
                </el-form-item> -->
                <el-form-item label="达标标准：">
                    <el-select v-model="editfrom.rsat">
                        <el-option label=" " value=""></el-option>
                        <el-option label="高校达标标准" value="高校达标标准"></el-option>
                    </el-select>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="cancelEdit">取 消</el-button>
                <el-button type="primary" @click="editGroupinfo">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
export default {
    data() {
        return {
            queryObj: {
                limit: 10,
                page: 1,
                query: " "
            },
            total: 0,
            currentPageData: [],
            multipleSelection: '',
            tableData: [],
            adddialogVisible: false,
            addfrom: {
                grid: null,
                name: '',
                gsize: 1,
                gsx: 0,
                gminus: 1,
                rsat: ''
            },
            initialForm: {},
            addgsx: false,
            editgsx: false,
            editdialogVisible: false,
            editfrom: {
                name: '',
                gsize: null,
                gsx: null,
                gminus: null,
                rsat: ''
            },
            rules: {
                grid: [
                    { required: true, message: '请输入编号', trigger: 'blur' },
                ],
                name: [
                    { required: true, message: '请输入组别名', trigger: 'blur' },
                ],
            }
        }
    },
    created() {
        this.initialForm = { ...this.addfrom }
        this.getGroupinfoList();
    },
    mounted() {
        this.addfrom.gsx = this.addgsx ? 1 : 0;
    },
    methods: {
        async getGroupinfoList() {
            try {
                const result = await this.$http.post(this.BACKEND_URL + '/match/getGroupmsg');
                if (result.data.code == 0) {
                    this.tableData = result.data.data
                    this.total = this.tableData.length
                    this.fetchData()
                } else if (result.data.code == 500) {
                    console.log(result.data.msg)
                }
            } catch (error) {
                if (error === undefined) {
                    this.$message.error('连接错误-获取数据失败');
                }
            }
        },
        // 勾选
        addleCheckboxChange(newValue) {
            this.addfrom.gsx = newValue ? 1 : 0;
        },
        editleCheckboxChange(newValue) {
            this.editfrom.gsx = newValue ? 1 : 0;
        },
        // 删除单条数据
        grodelete(e) {
            this.$confirm('此操作将删除改条数据，是否继续', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning',
                center: true
            }).then(async () => {
                const result = await this.$http.post(this.BACKEND_URL + '/match/deleteGroupmsg', { igroup: e.igroup, grid: e.grid })
                if (result.data.code === 0) {
                    this.$message({
                        type: 'success',
                        message: '删除成功!'
                    });
                    this.getGroupinfoList();
                } else if (result.data.code == 500) {
                    this.$message.error(result.data.msg)
                }
            }).catch(() => {
            });
        },
        // 获取单个数据信息
        async showeditdialog(info) {
            this.editdialogVisible = true
            this.editfrom = info
            this.editgsx = this.editfrom.gsx === 1;
        },
        //修改
        async editGroupinfo() {
            const result = await this.$http.post(this.BACKEND_URL + '/match/updateGroupmsg', this.editfrom)
            if (result.data.code == 0) {
                this.editdialogVisible = false
                this.getGroupinfoList()
                this.$message.success('修改成功')
            } else if (result.data.code == 500) {
                this.getGroupinfoList()
                this.$message.error(result.data.msg)
            }
        },
        cancelEdit() {
            this.editdialogVisible = false
            this.getGroupinfoList()
        },
        handleClose(done) {
            this.getGroupinfoList()
            done()
        },
        // 新增数据
        async addgro() {
            this.$refs.addForm.validate(async (valid) => {
                if (valid) {
                    const result = await this.$http.post(this.BACKEND_URL + '/match/insertGroupmsg', this.addfrom)
                    if (result.data.code == 0) {
                        this.adddialogVisible = false
                        this.getGroupinfoList()
                        this.$message.success('添加成功')
                        this.addfrom = { ...this.initialForm }
                    } else if (result.data.code == 500) {
                        this.addfrom = { ...this.initialForm }
                        this.$message.error(result.data.msg)
                    }
                } else {
                    this.$message.error('请填写必要信息');
                }
            });
        },
        addleClose() {
            this.adddialogVisible = false
            this.addfrom = { ...this.initialForm }
        },
        // 分页
        fetchData() {
            this.currentPageData = this.tableData.slice(0, this.queryObj.limit);
            this.queryObj.page = 1
        },
        handleSizeChange(newSize) {
            this.queryObj.limit = newSize
        },
        handlePageChange(newPage) {
            this.queryObj.page = newPage;
            let startIndex = (newPage - 1) * this.queryObj.limit;
            let endIndex = startIndex + this.queryObj.limit;
            this.currentPageData = this.tableData.slice(startIndex, endIndex);
        }
    }
}
</script>

<style lang="less" scoped>
.box-card {
    margin-top: 15px;

    .box-top {
        float: left;
        margin-bottom: 15px;
    }
}

// 操作按钮
:deep(.cell) {
    text-align: center;
}

.el-dialog {
    .el-checkbox {
        margin-left: 50px;
    }
}

.paging {
    margin-top: 15px;
}
</style>